const AC_GAME_OBJECTS = []

export class AcGameObject {
  constructor () {
    AC_GAME_OBJECTS.push(this)
    this.timeDelta = 0
    this.has_called_start = false
  }

  start () {

  }

  update () {

  }

  on_destroy() {
    
  }

  destroy () {

    this.on_destroy()
    for (let i = 0; i < AC_GAME_OBJECTS.length; i++) {
      if (this === AC_GAME_OBJECTS[i]) {
        AC_GAME_OBJECTS.splice(i, 1)
        break
      }
    }
  }
}

let last_timestamp = 0
const step = timestamp => {
  for (const obj of AC_GAME_OBJECTS) {
    if (!obj.has_called_start) {
      obj.start()
      obj.has_called_start = true
    } else {
      obj.timeDelta = timestamp - last_timestamp
      obj.update()
    }
  }
  last_timestamp = timestamp
  requestAnimationFrame(step)
}

requestAnimationFrame(step)
